package com.yellowpage.white.struts;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.yellowpage.ca.data.User;
import com.yellowpage.ca.service.UserService;

import com.yellowpage.white.common.Constants;



public class LoginServlet extends HttpServlet{
    //日志记录
    private Log log = LogFactory.getLog(LoginServlet.class);
    private static final long serialVersionUID = -590134883280279977L;
    
    private String message;
    

    public String getMessage() {
		return message;
	}


	public void setMessage(String message) {
		this.message = message;
	}
	
	private User user;
	private UserService userService;
	


	public User getUser() {
		return user;
	}


	public void setUser(User user) {
		this.user = user;
	}


	public UserService getUserService() {
		return userService;
	}


	public void setUserService(UserService userService) {
		this.userService = userService;
	}


	public void service(HttpServletRequest request,HttpServletResponse response){
        HttpSession session = request.getSession();
        
        System.out.println("****** LoginServlet service() invoke .... ");
        
        String loginName=request.getParameter(Constants.get("USER_LOGINNAME"));//"j_username"
        String password=request.getParameter(Constants.get("USER_PASSWORD"));	//"j_password"
        
        System.out.println("****** LoginServlet service() loginName = " + loginName);
        System.out.println("****** LoginServlet service() password = " + password);
        
        if(loginName!=null){
            session.setAttribute(Constants.get("USER_LOGINNAME"),loginName);	//"j_username"            
        }else{
            loginName=(String)session.getAttribute(Constants.get("USER_LOGINNAME"));	// "j_username"
        }
        
               
        if(password!=null){
            session.setAttribute(Constants.get("USER_PASSWORD"),password);	// "j_password"
        }else{
            password=(String)session.getAttribute(Constants.get("USER_PASSWORD"));	// "j_password"
        }
        
        String spId=request.getParameter(Constants.get("USER_SPID"));	// "j_spId"
        
        

        ServletContext servletContext = session.getServletContext();
        WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
        if(spId == null){
            UserService userService = (UserService)ctx.getBean("userService");
            User user = userService.getUserByName(loginName);           
            
            
            if(user != null){
            	
            	List functionList = userService.getFunctionsByUser(user.getUserId());
                
            	session.setAttribute(Constants.get("USER_NAME"),user.getName());	// "username"
                session.setAttribute(Constants.get("USER_ID"),user.getUserId());	// "userId"
                session.setAttribute(Constants.get("USER_OBJECT"),user);
                session.setAttribute("FUNC_LIST",functionList);
                session.setAttribute("displayname",user.getDisplayName());	// "userId"
            }else{
            	request.setAttribute("message","登录名不存在");
            }
            
          
            
        }

        PrintWriter pw = null;
        try{
            response.setCharacterEncoding("UTF-8");
            response.setContentType("text/html");

            pw = response.getWriter();
            pw.append("<html>");
            pw.append(
                    "       <head>" +
                            "          <script type=\"text/javascript\">" +
                            "               window.onload=closeWindow;" +
                            "               function closeWindow(){" +
                            "                      var loginForm = document.getElementById(\"loginForm\");" +
                            "                      loginForm.submit();" +
                            "                                      }" +
                            "          </script>" +
                            "       </head>"
            );
            pw.append(" " +
                    "      <body>" +
                    "          <form method=\"post\" id=\"loginForm\" action=\""+request.getContextPath() +"/j_acegi_security_check\" >" +
                    "              <input type=\"hidden\" name=\"j_username\" value=\""+loginName+"\"/>" +
                    "              <input type=\"hidden\" name=\"j_password\" value=\""+password+"\"/>"+
                    "         </form>" +
                    "      </body>"
            );

            pw.append("</html>");
            pw.flush();
        }catch(IOException e){
            log.info("在用户登陆时有异常抛出:"+ e.getMessage());
        }finally{
            if(pw != null){
                pw.close();
            }
        }
    }

}
